package net.borderwars.map;

/**
 * User: Eric
 * Date: Nov 21, 2004
 * Time: 8:20:58 PM
 */
public class Velocity {
    private int vy = Integer.MAX_VALUE;
    private int vx = Integer.MAX_VALUE;

    private long speed = Integer.MIN_VALUE;
    private double direction = Double.NaN;

    /**
     * Create a velocity object in terms of speed and direction
     *
     * @param speed     speed
     * @param direction The direction in radians
     */
    public Velocity (long speed, double direction) {
        this.speed = speed;
        this.direction = direction;
        vx = (int) (speed * Math.cos (direction));
        vy = (int) (speed * Math.sin (direction));
    }

    public Velocity (int vx, int vy) {
        this.vx = vx;
        this.vy = vy;
        rectFromPolar (vx, vy);
    }

    public long getSpeed () {
        return (speed);
    }

    public double getDirection () {
        return (direction);
    }

    public int getVX () {
        return (vx);
    }

    public int getVY () {
        return (vy);
    }

    private void rectFromPolar (int vx, int vy) {
        direction = Math.atan2 (vx, vy);
        speed = (int) Math.sqrt (Math.pow (vx, 2) + Math.pow (vy, 2));
    }

    public long timeBetween (Location start, Location dest) {
        return (start.distance (dest) / speed);
    }

    public String toString () {
        return "Velocity{" + "vy=" + vy + ", vx=" + vx + ", speed=" + speed + ", direction=" + direction + "}";
    }
}
